package controler;

import java.util.Observable;

import javax.swing.JList;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import template.AbstractListener;

import model.Path;

import utils.Dom;

import org.jdom.Element;

public class ShowDeliveryPathListener extends AbstractListener implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	private JList deliveriesOrderList;
	private JTextField emplacementTextField;
	private String buffer;
	public ShowDeliveryPathListener(Observable obs, JList jl, JTextField jtf){
		addModel(obs);
		deliveriesOrderList = jl;
		emplacementTextField = jtf;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
	  Path pm = (Path)getModel(0);
	  
	  String startPoint = emplacementTextField.getText();
	  
	  Element root = Dom.getXMLFile("bookOfDelivery.xml").getRootElement();
	  Element selectedDelivery = root.getChild("delivery"+(deliveriesOrderList.getSelectedIndex()+1));
	  
	  Element restRoot = Dom.getXMLFile("restaurants.xml").getRootElement();
	  	buffer = selectedDelivery.getChildText("restaurant").replaceAll(" ","").toLowerCase().trim();
	  Element seclectedRestAddress = restRoot.getChild(buffer);
	  	
	  String stopPoint = seclectedRestAddress.getChild("address").getChildText("civicaddress")+","+selectedDelivery.getChildText("city")+","+selectedDelivery.getChildText("country");
	  String endPoint = selectedDelivery.getChildText("civicaddress")+","+selectedDelivery.getChildText("city")+","+selectedDelivery.getChildText("country");
	  	  
	  pm.setPoints(startPoint, stopPoint, endPoint);
	}
}
